package org.neuedu.mq;

import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.apache.logging.log4j.message.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

@Component
public class LzwEmailListener {
    @Value("${spring.mail.username}")
    String from;
    @Resource
    JavaMailSender javaMailSender;
    @RabbitListener(queues = "admin")
    public void emailHandler(String message) throws MessagingException {
        NeuMail neuMail= JSONObject.parseObject(message,NeuMail.class);
        final MimeMessage mimeMessage= javaMailSender.createMimeMessage();
        MimeMessageHelper helper=new MimeMessageHelper(mimeMessage);
        helper.setTo(neuMail.getTo());
        helper.setSubject(neuMail.getSubject());
        helper.setText(neuMail.getText());
        helper.setFrom(from);
        javaMailSender.send(mimeMessage);
    }
}
